/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 连接数据库2;


  
import java.io.IOException;
import java.sql.*;
import java.util.*; 
import static java.lang.System.out;
import java.util.Scanner;

public class MessageDAODemo {
    public static void main (String[] args) throws Exception{
        MessageDAO dao = new MessageDAO(
            "jdbc:mysql;//localhost:3306/demo?"+
                    "useUnicode=true&characterEncoding=UTF8",
        "root","");
        //在Windows的Netbeans中，其Output窗口的标准输入编码是Big5
       Scanner console = new Scanner(System.in,"Big5");
       while(true){
       out.print("(1) 显示留言 （2） 新增留言:");
       switch(Integer.parseInt(console.nextLine())){
           case 1:
           dao.get().forEach(message -> {
                out.printf("%d\t%s\t%s\t%s%n",
                        message.getId(),
                        message.getName(),
                        message.getEmail(),
                        message.getMsg());
           });
           break;
           case 2:
               out.print("姓名：");
               String name = console.nextLine();
               out.print("邮件：");
               String email = console.nextLine();
               out.print("留言：");
               String msg = console.nextLine();
               dao.add(new Message(name, email, msg));
       }
       }
    }
}

  

